package com.winter.util;

import com.google.common.collect.ImmutableMap;

import java.util.Optional;

/**
 * 描述：
 *
 * @author He Yong
 * @version 1.0
 * @since 2017/12/24
 */
public class StringUtils extends org.apache.commons.lang3.StringUtils {

    private static final char SEPARATOR = '_';

    private final static String[] agent = {"Android", "iPhone", "iPod", "iPad", "Windows Phone", "MQQBrowser"};

    private static final ImmutableMap<Character, String> QUOTED_STRING_ESCAPE = ImmutableMap.<Character, String>builder()
            .put('\\', "\\\\")
            .put('\"', "\\\"")
            .put('\'', "\\\'")
            .put('\r', "\\r")
            .put('\n', "\\n")
            .put('\f', "\\f")
            .put('\t', "\\t")
            .put('\b', "\\b")
            .put('\u00a0', " ")
            .build();

    /**
     * 驼峰命名法工具
     *
     * @return toCamelCase(" hello_world ") == "helloWorld"
     * toCapitalizeCamelCase("hello_world") == "HelloWorld"
     * toUnderScoreCase("helloWorld") = "hello_world"
     */
    public static String toCamelCase(String s) {
        if (s == null) {
            return null;
        }

        s = s.toLowerCase();

        StringBuilder sb = new StringBuilder(s.length());
        boolean upperCase = false;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);

            if (c == SEPARATOR) {
                upperCase = true;
            } else if (upperCase) {
                sb.append(Character.toUpperCase(c));
                upperCase = false;
            } else {
                sb.append(c);
            }
        }

        return sb.toString();
    }

    /**
     * 驼峰命名法工具
     *
     * @return toCamelCase(" hello_world ") == "helloWorld"
     * toCapitalizeCamelCase("hello_world") == "HelloWorld"
     * toUnderScoreCase("helloWorld") = "hello_world"
     */
    public static String toUnderScoreCase(String s) {
        if (s == null) {
            return null;
        }

        StringBuilder sb = new StringBuilder();
        boolean upperCase = false;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);

            boolean nextUpperCase = true;

            if (i < (s.length() - 1)) {
                nextUpperCase = Character.isUpperCase(s.charAt(i + 1));
            }

            if ((i > 0) && Character.isUpperCase(c)) {
                if (!upperCase || !nextUpperCase) {
                    sb.append(SEPARATOR);
                }
                upperCase = true;
            } else {
                upperCase = false;
            }
            sb.append(Character.toLowerCase(c));
        }

        return sb.toString();
    }

    /**
     * 输入字符是否包含关键字 备注：以英式逗号,分割
     */
    public static boolean containKeyword(String source, String keyword,
                                         boolean isIgnoreCase) {
        if (isBlank(keyword) || isBlank(source))
            return false;
        if (isIgnoreCase) {
            keyword = keyword.toUpperCase();
            source = source.toUpperCase();
        }
        return (",".concat(source)
                   .concat(",")).contains((",".concat(keyword)
                                              .concat(",")));
    }

    public static boolean equalsTrim(String s1, String s2) {
        s1 = trim(s1) == null ? "" : trim(s1);
        s2 = trim(s2) == null ? "" : trim(s2);
        return s1.equals(s2);
    }

    /**
     * 判断User-Agent 是不是来自于手机
     * String ua = request.getHeader("User-Agent");
     */
    public static boolean checkAgentIsMobile(String ua) {
        boolean flag = false;
        if (!ua.contains("Windows NT") || (ua.contains("Windows NT") && ua.contains("compatible; MSIE 9.0;"))) {
            // 排除 苹果桌面系统
            if (!ua.contains("Windows NT") && !ua.contains("Macintosh")) {
                for (String item : agent) {
                    if (ua.contains(item)) {
                        flag = true;
                        break;
                    }
                }
            }
        }
        return flag;
    }

    /**
     * 转义为""中可用的字符串，诸如\n \t等将会被转义
     */
    public static String escapeQuotedString(String source) {
        if (isNotEmpty(source)) {
            StringBuilder sb = new StringBuilder(source.length() + 16);
            for (int i = 0; i < source.length(); i++) {
                char ch = source.charAt(i);
                sb.append(Optional.ofNullable(QUOTED_STRING_ESCAPE.get(ch))
                                  .orElse(String.valueOf(ch)));
            }
            return sb.toString();
        }
        return "";
    }


}
